package com.ce.pms.system.custservice.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.custservice.dao.ISeriousDegreeDao;
import com.ce.pms.system.custservice.model.SeriousDegreeDO;
import com.ce.pms.system.custservice.service.ISeriousDegreeService;
import com.ce.pms.system.custservice.vo.SeriousDegreeVO;
import com.ce.util.BeanUtil;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;
import com.sitechasia.webx.core.utils.base.PopulateUtil;
/**
 * 功能描述: 严重程度Service实现类
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author  胡煦
 * Create on:2008-12-18
 * Edit author:
 * Edit on:
 * 修改原因：

 */

public class SeriousDegreeServiceImpl extends PmsBaseServiceImpl implements
		ISeriousDegreeService {

	/**
	 * 方法描述: 分页查询所有严重程度列表
	 * @author 胡煦
	 * Create on:2008-7-30
	 * @param pageNo,pageSize,order
	 * @return 返回当前页的查询结果
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public Page queryAllSeriousDegreeByPage(SearchVO searchVO){
		Page page = seriousDegreeDao.queryAllSeriousDegreeByPage(searchVO);
		List<SeriousDegreeDO> seriousDegreeDOList = page.getResult();
		List<SeriousDegreeVO> seriousDegreeVOList = new ArrayList<SeriousDegreeVO>();
		for(SeriousDegreeDO seriousDegreeDO : seriousDegreeDOList){
			SeriousDegreeVO seriousDegreeVO = this.assembleD2V(seriousDegreeDO, null);
			seriousDegreeVOList.add(seriousDegreeVO);
		}
		page.setResult(seriousDegreeVOList);
		return page;
	}
	/**
	 * 方法描述: 严重程度列表查询方法
	 * @author 胡煦
	 * Create on:2008-12-16
	 * @param name
	 * @return List<SeriousDegreeVO>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public List<SeriousDegreeVO> getSeriousDegreeList() {
		Page page = seriousDegreeDao.getSeriousDegreeList(null);
		List<SeriousDegreeDO> seriousDegreeDOList = page.getResult();
		List<SeriousDegreeVO> seriousDegreeVOList = new ArrayList<SeriousDegreeVO>();
		for(SeriousDegreeDO seriousDegreeDO : seriousDegreeDOList){
			SeriousDegreeVO seriousDegreeVO = this.assembleD2V(seriousDegreeDO, null);
			seriousDegreeVOList.add(seriousDegreeVO);
		}
		return seriousDegreeVOList;
	}
	/**
	 * 方法描述: 根据ID查询VO方法
	 * @author 胡煦
	 * Create on:2008-12-22
	 * @param keyword
	 * @return SeriousDegreeVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public SeriousDegreeVO findSeriousDegreeVOById(Long keyword) {
		if(BlankUtil.isBlank(keyword)){
			return null;
		}
		return this.assembleD2V(seriousDegreeDao.findById(keyword), null);
	}
	/**
	 * 方法描述: 严重程度增加方法
	 *
	 * @author 胡煦 Create on:2008-12-16
	 * @param seriousDegreeVO
	 * @return SeriousDegreeVO Edit author: Edit on: 修改原因：
	 */
	public SeriousDegreeVO addSeriousDegree(SeriousDegreeVO seriousDegreeVO) {
		if(BlankUtil.isBlank(seriousDegreeVO)){
			return null;
		}
		this.checkSeriousDegreeByName(seriousDegreeVO.getName(), null);
		SeriousDegreeDO seriousDegreeDO = this.assembleV2D(seriousDegreeVO, null);
		seriousDegreeDO = seriousDegreeDao.saveObject(seriousDegreeDO);
		seriousDegreeVO.setKeyword(seriousDegreeDO.getKeyword());
		return seriousDegreeVO;
	}
	/**
	 * 方法描述: 严重程度删除方法
	 *
	 * @author 胡煦 Create on:2008-12-16
	 * @param seriousDegreeId
	 * @return boolean Edit author: Edit on: 修改原因：
	 */
	public boolean deleteSeriousDegree(Long seriousDegreeId) {
		if(BlankUtil.isBlank(seriousDegreeId)){
			return false;
		}
		try {
			seriousDegreeDao.deleteObjectById(seriousDegreeId);
		} catch (RuntimeException e) {
			e.printStackTrace();
			throw new BusinessException(
					"custservice:system_delete_inused_exception",
					BusinessException.WARNING);
		}
		return true;
	}
	/**
	 * 方法描述: 严重程度修改方法
	 *
	 * @author 胡煦 Create on:2008-12-16
	 * @param seriousDegreeDO
	 * @return SeriousDegreeDO Edit author: Edit on: 修改原因：
	 */
	public SeriousDegreeVO editSeriousDegree(SeriousDegreeVO seriousDegreeVO) {
		if(BlankUtil.isBlank(seriousDegreeVO)){
			return null;
		}
		this.checkSeriousDegreeByName(seriousDegreeVO.getName(), seriousDegreeVO.getKeyword());
		SeriousDegreeDO seriousDegreeDO = this.assembleV2D(seriousDegreeVO, null);
		SeriousDegreeDO oldSeriousDegreeDO = seriousDegreeDao.findById(seriousDegreeVO.getKeyword());
		modifyInfo(oldSeriousDegreeDO.getSysInfo(), seriousDegreeVO.getModifier());
		BeanUtil.convertVoAsDo(seriousDegreeDO, oldSeriousDegreeDO);
		return seriousDegreeVO;
	}
	/**
	 * 功能描述:校验严重程度名字是否存在<br>
	 * 注：新建判断name的唯一性时，oldId传为null；修改判断name的唯一性时，则需要传入本身的ID<br>
	 * @param name
	 * @param id
	 * @return
	 * Create author:胡煦
	 * Create on:2008-7-18
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void checkSeriousDegreeByName(String name,Long id){
		Long count = seriousDegreeDao.countSeriousDegreeByName(name, id);
		if(count > 0){
			throw new BusinessException(
					"custservice:system_name_exist_exception",
					BusinessException.WARNING);
		}
	}

	/**
	 * 方法描述: SeriousDegreeVO To SeriousDegreeDO
	 * @author 胡煦
	 * Create on:2008-12-16
	 * @param SeriousDegreeVO
	 * @param SeriousDegreeDO
	 * @return void
	 */
	private SeriousDegreeDO assembleV2D(SeriousDegreeVO seriousDegreeVO, SeriousDegreeDO seriousDegreeDO) {
		if(BlankUtil.isBlank(seriousDegreeDO)){
			seriousDegreeDO = new SeriousDegreeDO();
		}
		BeanUtil.convertVoAsDo(seriousDegreeVO, seriousDegreeDO);
		if(!BlankUtil.isBlank(seriousDegreeVO.getCreator())){
			seriousDegreeDO.setSysInfo(createInfo(seriousDegreeVO.getCreator()));
		}
		return seriousDegreeDO;
	}

	/**
	 * 方法描述: SeriousDegreeDO To SeriousDegreeVO
	 * @author 胡煦
	 * Create on:2008-12-16
	 * @param SeriousDegreeDO
	 * @param SeriousDegreeVO
	 * @return SeriousDegreeVO
	 */
	private SeriousDegreeVO assembleD2V(SeriousDegreeDO seriousDegreeDO, SeriousDegreeVO seriousDegreeVO) {
		if(BlankUtil.isBlank(seriousDegreeVO)){
			seriousDegreeVO = new SeriousDegreeVO();
		}
		PopulateUtil.populate(seriousDegreeDO, seriousDegreeVO);
		return seriousDegreeVO;
	}
	private ISeriousDegreeDao seriousDegreeDao;
	/**
	 * @param seriousDegreeDao the seriousDegreeDao to set
	 */
	public void setSeriousDegreeDao(ISeriousDegreeDao seriousDegreeDao) {
		this.seriousDegreeDao = seriousDegreeDao;
	}

}
